/**
 * 
 */
package importExport;

import importExport.log.WuselLogger;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Properties;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import dataTypes.Constants;
import dataTypes.Disease;

/**
 * This class provides methods for import and export of disease data
 * 
 * @author Kevin Wagner
 * @version 1.0
 * 
 */
public class DiseaseImportExport {

	/**
	 * 
	 * @param props 
	 * @return a list of diseases
	 */
	public static ArrayList<Disease> importDisease(Properties props) {
		ArrayList<Disease> temp = new ArrayList<Disease>();
		File dir = new File(props.getProperty("diseaseDir", "Disease"));
		if(!dir.exists()||!dir.isDirectory()){
			WuselLogger.log.warn("No Disease Directory found!");
			return temp;
		}
		File[] diseaseFiles = dir.listFiles(new FileFilter() {

			@Override
			public boolean accept(File pathname) {
				return pathname.isFile()
						&& pathname.getName().endsWith(".disease");
			}
		});
		for (int i = 0; i < diseaseFiles.length; i++) {
			temp.add(importDisease(diseaseFiles[i]));
			if (temp.get(i) == null) {
				temp.remove(null);
			}
		}
		return temp;
	};

	private static Disease importDisease(File filename) {
		Disease d = new Disease();
		try {
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(filename);
			NodeList nList = doc.getElementsByTagName("Name");
			d.setName(nList.item(0).getTextContent());
			nList = doc.getElementsByTagName("IncubationTime");
			d.setIncubationTime(Integer
					.parseInt(nList.item(0).getTextContent()));
			nList = doc.getElementsByTagName("IllTime");
			d.setIllTime(Integer.parseInt(nList.item(0).getTextContent()));
			nList = doc.getElementsByTagName("ImmuneTime");
			d.setImmuneTime(Integer.parseInt(nList.item(0).getTextContent()));
			nList = doc.getElementsByTagName("InfectionProbabilty");
			d.setInfectionProbability(Double.parseDouble(nList.item(0)
					.getTextContent()));
			nList = doc.getElementsByTagName("MortalityProbabilty");
			d.setMortalityProbability(Double.parseDouble(nList.item(0)
					.getTextContent()));
			nList = doc.getElementsByTagName("InfectionDistance");
			d.setInfectionDistance(Short.parseShort(nList.item(0)
					.getTextContent()));
		} catch (Exception e) {
			WuselLogger.log.error(e.getLocalizedMessage());
			return null;
		}
		return d;
	}

	/**
	 * Exports a list of diesease
	 * 
	 * @param disease
	 * @param props 
	 * @return status of exporting
	 */
	public static boolean exportDisease(ArrayList<Disease> disease, Properties props) {
		File f = new File(props.getProperty("diseaseDir", "Disease"));
		if (!f.exists()||f.isDirectory()) {
			f.mkdir();
		}
		for (int i = 0; i < disease.size(); i++) {
			if (!exportDisease(disease.get(i), f)) {
				WuselLogger.log.error("Error in Exporting Disease");
				return false;
			}

		}
		return true;
	}

	/**
	 * @param disease
	 * @param dir
	 * @return status of saving
	 */
	public static boolean exportDisease(Disease disease, File dir) {
		File temp = new File(dir.getAbsolutePath()
				+ Constants.FILESEPARATOR + disease.getName()
				+ ".disease");
		try {
			FileWriter fw = new FileWriter(temp, false);
			BufferedWriter bw = new BufferedWriter(fw);
			bw.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>");
			bw.newLine();
			bw.append("<Disease>");
			bw.newLine();
			bw.append("\t<Name>" + disease.getName() + "</Name>");
			bw.newLine();
			bw.append("\t<IncubationTime>" + disease.getIncubationTime()
					+ "</IncubationTime>");
			bw.newLine();
			bw.append("\t<IllTime>" + disease.getIllTime() + "</IllTime>");
			bw.newLine();
			bw.append("\t<ImmuneTime>" + disease.getImmuneTime()
					+ "</ImmuneTime>");
			bw.newLine();
			bw.append("\t<InfectionProbabilty>"
					+ disease.getInfectionProbability()
					+ "</InfectionProbabilty>");
			bw.newLine();
			bw.append("\t<MortalityProbabilty>"
					+ disease.getMortalityProbability()
					+ "</MortalityProbabilty>");
			bw.newLine();
			bw.append("\t<InfectionDistance>" + disease.getInfectionDistance()
					+ "</InfectionDistance>");
			bw.newLine();
			bw.append("</Disease>");
			bw.flush();
			bw.close();
		} catch (Exception e) {
			WuselLogger.log.error(e.getLocalizedMessage());
			return false;
		}
		return true;
	}

	/**
	 * @param dis
	 * @param props 
	 * @return status of deleting
	 */
	public static boolean deleteDiseaseFile(Disease dis, Properties props) {
		File dir = new File(props.getProperty("diseaseDir", "Disease"));
		File f=new File(dir.getPath()+Constants.FILESEPARATOR+dis.getName()+".disease");
		if(!f.exists()){
			return true;
		}
		return f.delete();
	}

}
